<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="module-gzip.html" />
<link rel="prev" href="archiving.html" />
<link rel="parent" href="archiving.html" />
<link rel="next" href="module-gzip.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>12.1 zlib -- Compression compatible with gzip</title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="12. data Compression and"
  href="archiving.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="12. data Compression and"
  href="archiving.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="12.2 gzip  "
  href="module-gzip.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="archiving.html">12. Data Compression and</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="archiving.html">12. Data Compression and</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-gzip.html">12.2 gzip  </a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h1><a name="SECTION0014100000000000000000">
12.1 <tt class="module">zlib</tt> --
         Compression compatible with <b class="program">gzip</b></a>
</h1>

<p>
<a name="module-zlib"></a>

<p>
For applications that require data compression, the functions in this
module allow compression and decompression, using the zlib library.
The zlib library has its own home page at <a class="url" href="http://www.zlib.net">http://www.zlib.net</a>.  
There are known incompatibilities between the Python module and
versions of the zlib library earlier than 1.1.3; 1.1.3 has a security
vulnerability, so we recommend using 1.1.4 or later.

<p>
zlib's functions have many options and often need to be used in a
particular order.  This documentation doesn't attempt to cover all of
the permutations; consult the zlib manual at
<a class="url" href="http://www.zlib.net/manual.html">http://www.zlib.net/manual.html</a> for authoritative information.

<p>
The available exception and functions in this module are:

<p>
<dl><dt><b><span class="typelabel">exception</span>&nbsp;<tt id='l2h-2318' xml:id='l2h-2318' class="exception">error</tt></b></dt>
<dd>
  Exception raised on compression and decompression errors.
</dd></dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2319' xml:id='l2h-2319' class="function">adler32</tt></b>(</nobr></td>
  <td><var>string</var><big>[</big><var>, value</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
   Computes a Adler-32 checksum of <var>string</var>.  (An Adler-32
   checksum is almost as reliable as a CRC32 but can be computed much
   more quickly.)  If <var>value</var> is present, it is used as the
   starting value of the checksum; otherwise, a fixed default value is
   used.  This allows computing a running checksum over the
   concatenation of several input strings.  The algorithm is not
   cryptographically strong, and should not be used for
   authentication or digital signatures.  Since the algorithm is
   designed for use as a checksum algorithm, it is not suitable for
   use as a general hash algorithm.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2320' xml:id='l2h-2320' class="function">compress</tt></b>(</nobr></td>
  <td><var>string</var><big>[</big><var>, level</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
  Compresses the data in <var>string</var>, returning a string contained
  compressed data.  <var>level</var> is an integer from <code>1</code> to
  <code>9</code> controlling the level of compression; <code>1</code> is fastest
  and produces the least compression, <code>9</code> is slowest and produces
  the most.  The default value is <code>6</code>.  Raises the
  <tt class="exception">error</tt> exception if any error occurs.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2321' xml:id='l2h-2321' class="function">compressobj</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>level</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
  Returns a compression object, to be used for compressing data streams
  that won't fit into memory at once.  <var>level</var> is an integer from
  <code>1</code> to <code>9</code> controlling the level of compression; <code>1</code> is
  fastest and produces the least compression, <code>9</code> is slowest and
  produces the most.  The default value is <code>6</code>.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2322' xml:id='l2h-2322' class="function">crc32</tt></b>(</nobr></td>
  <td><var>string</var><big>[</big><var>, value</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
  Computes a CRC (Cyclic Redundancy Check)<a id='l2h-2333' xml:id='l2h-2333'></a>
  checksum of <var>string</var>. If
  <var>value</var> is present, it is used as the starting value of the
  checksum; otherwise, a fixed default value is used.  This allows
  computing a running checksum over the concatenation of several
  input strings.  The algorithm is not cryptographically strong, and
  should not be used for authentication or digital signatures.  Since
  the algorithm is designed for use as a checksum algorithm, it is not
  suitable for use as a general hash algorithm.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2323' xml:id='l2h-2323' class="function">decompress</tt></b>(</nobr></td>
  <td><var>string</var><big>[</big><var>, wbits</var><big>[</big><var>, bufsize</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
  Decompresses the data in <var>string</var>, returning a string containing
  the uncompressed data.  The <var>wbits</var> parameter controls the size of
  the window buffer.  If <var>bufsize</var> is given, it is used as the
  initial size of the output buffer.  Raises the <tt class="exception">error</tt>
  exception if any error occurs.

<p>
The absolute value of <var>wbits</var> is the base two logarithm of the
size of the history buffer (the ``window size'') used when compressing
data.  Its absolute value should be between 8 and 15 for the most
recent versions of the zlib library, larger values resulting in better
compression at the expense of greater memory usage.  The default value
is 15.  When <var>wbits</var> is negative, the standard
<b class="program">gzip</b> header is suppressed; this is an undocumented feature
of the zlib library, used for compatibility with <b class="program">unzip</b>'s
compression file format.

<p>
<var>bufsize</var> is the initial size of the buffer used to hold
decompressed data.  If more space is required, the buffer size will be
increased as needed, so you don't have to get this value exactly
right; tuning it will only save a few calls to <tt class="cfunction">malloc()</tt>.  The
default size is 16384.

<p>
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2324' xml:id='l2h-2324' class="function">decompressobj</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>wbits</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
  Returns a decompression object, to be used for decompressing data
  streams that won't fit into memory at once.  The <var>wbits</var>
  parameter controls the size of the window buffer.
</dl>

<p>
Compression objects support the following methods:

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2325' xml:id='l2h-2325' class="method">compress</tt></b>(</nobr></td>
  <td><var>string</var>)</td></tr></table></dt>
<dd>
Compress <var>string</var>, returning a string containing compressed data
for at least part of the data in <var>string</var>.  This data should be
concatenated to the output produced by any preceding calls to the
<tt class="method">compress()</tt> method.  Some input may be kept in internal buffers
for later processing.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2326' xml:id='l2h-2326' class="method">flush</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>mode</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
All pending input is processed, and a string containing the remaining
compressed output is returned.  <var>mode</var> can be selected from the
constants <tt class="constant">Z_SYNC_FLUSH</tt>,  <tt class="constant">Z_FULL_FLUSH</tt>,  or 
<tt class="constant">Z_FINISH</tt>, defaulting to <tt class="constant">Z_FINISH</tt>.  <tt class="constant">Z_SYNC_FLUSH</tt> and 
<tt class="constant">Z_FULL_FLUSH</tt> allow compressing further strings of data, while
<tt class="constant">Z_FINISH</tt> finishes the compressed stream and 
prevents compressing any more data.  After calling
<tt class="method">flush()</tt> with <var>mode</var> set to <tt class="constant">Z_FINISH</tt>, the
<tt class="method">compress()</tt> method cannot be called again; the only realistic
action is to delete the object.  
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2327' xml:id='l2h-2327' class="method">copy</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
Returns a copy of the compression object.  This can be used to efficiently
compress a set of data that share a common initial prefix.

<span class="versionnote">New in version 2.5.</span>

</dl>

<p>
Decompression objects support the following methods, and two attributes:

<p>
<dl><dt><b><tt id='l2h-2328' xml:id='l2h-2328' class="member">unused_data</tt></b></dt>
<dd>
A string which contains any bytes past the end of the compressed data.
That is, this remains <code>""</code> until the last byte that contains
compression data is available.  If the whole string turned out to
contain compressed data, this is <code>""</code>, the empty string.

<p>
The only way to determine where a string of compressed data ends is by
actually decompressing it.  This means that when compressed data is
contained part of a larger file, you can only find the end of it by
reading data and feeding it followed by some non-empty string into a
decompression object's <tt class="method">decompress</tt> method until the
<tt class="member">unused_data</tt> attribute is no longer the empty string.
</dl>

<p>
<dl><dt><b><tt id='l2h-2329' xml:id='l2h-2329' class="member">unconsumed_tail</tt></b></dt>
<dd>
A string that contains any data that was not consumed by the last
<tt class="method">decompress</tt> call because it exceeded the limit for the
uncompressed data buffer.  This data has not yet been seen by the zlib
machinery, so you must feed it (possibly with further data
concatenated to it) back to a subsequent <tt class="method">decompress</tt> method
call in order to get correct output.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2330' xml:id='l2h-2330' class="method">decompress</tt></b>(</nobr></td>
  <td><var>string</var><big>[</big><var>, max_length</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Decompress <var>string</var>, returning a string containing the
uncompressed data corresponding to at least part of the data in
<var>string</var>.  This data should be concatenated to the output produced
by any preceding calls to the
<tt class="method">decompress()</tt> method.  Some of the input data may be preserved
in internal buffers for later processing.

<p>
If the optional parameter <var>max_length</var> is supplied then the return value
will be no longer than <var>max_length</var>. This may mean that not all of the
compressed input can be processed; and unconsumed data will be stored
in the attribute <tt class="member">unconsumed_tail</tt>. This string must be passed
to a subsequent call to <tt class="method">decompress()</tt> if decompression is to
continue.  If <var>max_length</var> is not supplied then the whole input is
decompressed, and <tt class="member">unconsumed_tail</tt> is an empty string.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2331' xml:id='l2h-2331' class="method">flush</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>length</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
All pending input is processed, and a string containing the remaining
uncompressed output is returned.  After calling <tt class="method">flush()</tt>, the
<tt class="method">decompress()</tt> method cannot be called again; the only realistic
action is to delete the object.

<p>
The optional parameter <var>length</var> sets the initial size of the
output buffer.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2332' xml:id='l2h-2332' class="method">copy</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
Returns a copy of the decompression object.  This can be used to save the
state of the decompressor midway through the data stream in order to speed up
random seeks into the stream at a future point.

<span class="versionnote">New in version 2.5.</span>

</dl>

<p>
<div class="seealso">
  <p class="heading">See Also:</p>

  <dl compact="compact" class="seemodule">
    <dt>Module <b><tt class="module"><a href="module-gzip.html">gzip</a></tt>:</b>
    <dd>Reading and writing <b class="program">gzip</b>-format files.
  </dl>
  <dl compact="compact" class="seeurl">
    <dt><a href="http://www.zlib.net"
        class="url">http://www.zlib.net</a></dt>
    <dd>The zlib library home page.</dd>
  </dl>
  <dl compact="compact" class="seeurl">
    <dt><a href="http://www.zlib.net/manual.html"
        class="url">http://www.zlib.net/manual.html</a></dt>
    <dd>The zlib manual explains 
    the semantics and usage of the library's many functions.</dd>
  </dl>
</div>

<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="12. data Compression and"
  href="archiving.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="12. data Compression and"
  href="archiving.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="12.2 gzip  "
  href="module-gzip.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="archiving.html">12. Data Compression and</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="archiving.html">12. Data Compression and</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-gzip.html">12.2 gzip  </a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
